Passing Buffer by reference

Can someone explain to me why the following code gives an error (at least in 8.3).
 

Buffer buf = null
 
void writeToBuf(Buffer &refBuf, string writeStr)
{
    if (null(refBuf))
        {
                refBuf = create()
                refBuf = ""
        }
 
        refBuf += writeStr "\n"
}
 
writeToBuf(buf, "Test1")
writeToBuf(buf, "Test2")
 
print stringOf(buf)

 


It gives DXL error:

 

 

 

-R-E- DXL: <Line:11> null buffer parameter was passed into argument position 1
Backtrace:
    <Line:14> 
-I- DXL: execution halted



I can work around this of course, but I just wonder what is going on. I think that it should work based on this description of the delete() function in the DXL Reference Manual:



 

 

 

 

void delete(Buffer &b)



Thanks!



 

 


SSloan - Fri Jun 11 16:47:01 EDT 2010

Re: Passing Buffer by reference
Regiss - Mon Jun 14 12:06:16 EDT 2010

Hi, this is because the refBuf in your code is not null.
That is &refBuf is a pointer to null, but this way null(refBuff) == false, since this pointer has some value, eg. 9213943... The question you'd like to ask is if the place where refBuf points has a value of null, so you'd need dereference operation.

I assume you don't wan to initialize the buffer in the beginning, so a quick and dirty fix would be:



Buffer buf = null

void writeToBuf(Buffer &refBuf, string writeStr)
{
Buffer b = refBuf

if ( null(b) ) { refBuf = create() }

refBuf += writeStr "\n"
}

writeToBuf(buf, "Test1")
writeToBuf(buf, "Test2")

print stringOf(buf)



I asked the same question in the thread:
References, function declarations and "underscore types" (_* _x etc.)

Re: Passing Buffer by reference
Regiss - Mon Jun 14 12:10:52 EDT 2010

Regiss - Mon Jun 14 12:06:16 EDT 2010
Hi, this is because the refBuf in your code is not null.
That is &refBuf is a pointer to null, but this way null(refBuff) == false, since this pointer has some value, eg. 9213943... The question you'd like to ask is if the place where refBuf points has a value of null, so you'd need dereference operation.

I assume you don't wan to initialize the buffer in the beginning, so a quick and dirty fix would be:



Buffer buf = null

void writeToBuf(Buffer &refBuf, string writeStr)
{
Buffer b = refBuf

if ( null(b) ) { refBuf = create() }

refBuf += writeStr "\n"
}

writeToBuf(buf, "Test1")
writeToBuf(buf, "Test2")

print stringOf(buf)



I asked the same question in the thread:
References, function declarations and "underscore types" (_* _x etc.)

Sorry for double posting. I just thought of another way to write this code:

if (null( (addr_ refBuf) Buffer) ) { ... }

Re: Passing Buffer by reference
SSloan - Mon Jun 14 20:22:42 EDT 2010

Regiss - Mon Jun 14 12:10:52 EDT 2010
Sorry for double posting. I just thought of another way to write this code:

if (null( (addr_ refBuf) Buffer) ) { ... }

I like where you are going and I think I tried something similar. I couldn't get it to work though and I'm still getting the same error with your code. :-( Here is the code that I ran.
 

Buffer buf = null
 
void writeToBuf(Buffer &refBuf, string writeStr)
{
    if (null( (addr_ refBuf) Buffer) )
        {
                refBuf = create()
        }
 
        refBuf += writeStr "\n"
}
 
writeToBuf(buf, "Test1")
writeToBuf(buf, "Test2")
 
print stringOf(buf)

 


I get this DXL error.

 

 

 

-R-E- DXL: <Line:10> null buffer parameter was passed into argument position 1
Backtrace:
    <Line:13> 
-I- DXL: execution halted



Not sure why this doesn't work.



 

 

Re: Passing Buffer by reference
SSloan - Mon Jun 14 20:24:58 EDT 2010

SSloan - Mon Jun 14 20:22:42 EDT 2010

I like where you are going and I think I tried something similar. I couldn't get it to work though and I'm still getting the same error with your code. :-( Here is the code that I ran.
 

Buffer buf = null
 
void writeToBuf(Buffer &refBuf, string writeStr)
{
    if (null( (addr_ refBuf) Buffer) )
        {
                refBuf = create()
        }
 
        refBuf += writeStr "\n"
}
 
writeToBuf(buf, "Test1")
writeToBuf(buf, "Test2")
 
print stringOf(buf)

 


I get this DXL error.

 

 

 

-R-E- DXL: <Line:10> null buffer parameter was passed into argument position 1
Backtrace:
    <Line:13> 
-I- DXL: execution halted



Not sure why this doesn't work.



 

 

I spoke too soon; Your first example works for me. It is very instructive, thanks!

Re: Passing Buffer by reference
Doug.Zawacki - Tue Jun 15 13:26:14 EDT 2010

SSloan - Mon Jun 14 20:24:58 EDT 2010
I spoke too soon; Your first example works for me. It is very instructive, thanks!

It seems the focus was slightly changed. Here is the code to resolve the original issue
Buffer buf = create()
buf = ""

void writeToBuf(Buffer &refBuf, string writeStr)
{
if (null(refBuf))
{
refBuf = ""
}

refBuf += writeStr "\n"
}

writeToBuf(buf, "Test1")
writeToBuf(buf, "Test2")

print stringOf(buf)